package com.darrenchan.leetcode;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2020-09-11 19:14
 */
public class XieMatrix2 {
    public static void main(String[] args) {
        int[][] a = new int[][]{{1,2,3,4}, {5,6,7,8}, {9,10,11,12}};
        printMatrix(a);
    }


    public static void printMatrix(int[][] a) {
        int m = a.length, n = a[0].length;
        int i = 0, j = n - 1, i2 = 0, j2 = n - 1;
        while(i <= m - 1 && j2 >= 0) {
            print(a, i, j, i2, j2);
            i = j == 0 ? i + 1 : i;
            j = j == 0 ? j : j - 1;
            //这个位置注意，要先写j2，因为j2用到i2，要是先写i2，i2值就变了
            j2 = i2 == m - 1 ? j2 - 1 : j2;
            i2 = i2 == m - 1 ? i2 : i2 + 1;
        }
    }

    public static void print(int[][] a, int i, int j, int i2, int j2) {
         while(i <= i2 && j <= j2){
             System.out.print(a[i][j] + " ");
             i++;
             j++;
         }
        System.out.println();
    }

}
